﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class AddScheduleTemplateDialog : ChildWindow
    {
        public ScheduleTemplateEditor ste;

        public AddScheduleTemplateDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text.Trim().Length > 0)
            {
                // Determine if the role already exists
                await Globals.db.GetScheduleTemplateObjects("Name='" + tbName.Text.Trim() + "'");
                if (Globals.db.ScheduleTemplateRecords.Count > 0) // Found record with the same name
                {
                    MessageBox.Show("Template name already exists.  Please choose a different title.");
                    return;
                }
                else
                {
                    // Add the new template
                    ServiceReferenceOpenRPOS.ScheduleTemplate st = new ServiceReferenceOpenRPOS.ScheduleTemplate();
                    st.col_Name = tbName.Text;
                    st.bFilterName = true;
                    await Globals.db.AddScheduleTemplateObject(st);
                }
                ste.init(ste.startDay);
                this.DialogResult = true;
            }
            else this.DialogResult = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

